<template>
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="ruleForm.roleName" />
      </el-form-item>
      <el-form-item label="权限选择" prop="permissionIds">
        <el-input v-model="ruleForm.permissionIds" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          新增角色权限
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
      </el-form-item>
    </el-form>
</template>
  
<script lang="ts" setup>
  import { reactive, ref,onMounted } from 'vue'
  import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
  import  { ElMessage, ElMessageBox } from 'element-plus'
  import axios from 'axios';
  
  interface RuleForm {
    roleName: string
    permissionIds: []
    
  }
  
  const formSize = ref<ComponentSize>('default')
  const ruleFormRef = ref<FormInstance>()
  const ruleForm = reactive<RuleForm>({
    roleName:'',
    permissionIds: []
  })
  
  

  const locationOptions = ['Home', 'Company', 'School']
  
  const rules = reactive<FormRules<RuleForm>>({
    roleName: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    permissionIds: [
      {
        required: true,
        message: '请选择权限',
        trigger: 'blur',
      },
    ],
  })
  
  const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
      if (valid) {
        console.log('submit!')
        axios.post('http://localhost:5044/api/RBAC/AddRoleToPermissions',ruleForm)
        .then(res=>{
            console.log(res)
            if(res.data.isSuc)
            {
                ElMessage.success('新增角色权限成功')
            }
            else{
                ElMessage.error(res.data.msg)
                return
            }
        })
        .catch(err=>{
            console.log(err)
        })
      } else {
        console.log('error submit!', fields)
      }
    })
  }
  
  const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
  }
  
  const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
  }))
</script>
  